package com.lyluly.coursemgmt.webmodule.controller;

import com.lyluly.coursemgmt.servicemodule.iService.ITeacherService;
import com.lyluly.coursemgmt.servicemodule.model.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @version v1.0
 * @author clc
 */
@Controller
@RequestMapping("/teachers")
public class TeacherController {
    @Autowired
    private ITeacherService iTeacherService;

    /**
     * 查找所有教师
     * @return 返回所有教师
     */
    @GetMapping
    @ResponseBody
    public List<Teacher> findTeacherList(){
        List<Teacher> teacherList = iTeacherService.findTeacherList();
        return teacherList;
    }

    /**
     * 根据教师ID查找一个教师
     * @param tId 教师ID
     * @return 返回相应教师
     */
    @GetMapping("/tId/{tId}")
    @ResponseBody
    public Teacher findTeacherById(@PathVariable String tId){
        Teacher teacher = iTeacherService.findTeacherById(tId);
        return teacher;
    }

    /**
     * 根据教师姓名查找若干个教师
     * @param tName 教师姓名
     * @return 返回相应教师
     */
    @GetMapping("/tName/{tName}")
    @ResponseBody
    public List<Teacher> findTeacherListByName(@PathVariable String tName){
        List<Teacher> teacherList = iTeacherService.findTeacherListByName(tName);
        return teacherList;
    }

    /**
     * 增加一个教师
     * @param teacher 教师实体类
     * @param response
     */
    @PostMapping
//    @ResponseBody
    /*只声明void，会默认还是访问teachers.jsp，解决方法：
    * 1、加上@ResponseBody
    * 2、传入HttpServletResponse参数*/
    public void addTeacher(@RequestBody Teacher teacher, HttpServletResponse response){
        iTeacherService.addTeacher(teacher);
    }

    /**
     * 根据教师ID修改一个教师
     * @param teacher 教师实体类
     * @param response
     */
    @PutMapping
    public void modifyTeacher(@RequestBody Teacher teacher, HttpServletResponse response){
        iTeacherService.modifyTeacher(teacher);
    }

    /**
     * 根据教师ID删除一个教师
     * @param tId 教师ID
     * @param response
     */
    @DeleteMapping("/{tId}")
    public void removeTeacherById(@PathVariable String tId, HttpServletResponse response){
        iTeacherService.removeTeacherById(tId);
    }
}
